﻿CREATE PROCEDURE [dbo].[proc_Project_Getlist_fyyxsr]
	(
		@CompanyId int,
		@StartDate nvarchar(10),
		@EndDate nvarchar(10),
		@Audit int,
		@Stext nvarchar(50),
		@StartIndex int,
		@EndIndex INT,
		@TypeId INT,
		@noe_flag INT
	)
AS
Begin
	;WITH list as (
		
		SELECT ROW_NUMBER() OVER ( ORDER BY CusName)AS Row,*	from ( select OrderId,CusName,SUM(a) AS a FROM (
		Select 
		id, OrderId
		,CusName
		,(SalePrice-CostPrice) AS a
		From Project
 WHERE  ( CompanyId=@CompanyId) and Convert(nchar(10),CreateDate,120) between @StartDate and @EndDate
		and SalePrice<>CostPrice AND ProductId IN (22,29,33)
		) AS ab  GROUP BY OrderId,CusName ) AS nb
	)
		
	Select *,(Select Count(1) From list) AS DataCount
		,(Select Sum(a) From list) AS CostPrice_SUM 
--(Select Sum(SalePrice-CostPrice) From Project WHERE TypeId= @TypeId  and ( CompanyId=@CompanyId) and Convert(nchar(10),CreateDate,120) between @StartDate and @EndDate) AS CostPrice_SUM 	
	From list Where Row between @StartIndex and @EndIndex   
End
	RETURN
